import Game, sys

PlaySpace = Game.Board('loc')

players = Game.initPlayers(4,PlaySpace,('hand','cardsTaken'),('score'))
for x in range(4):
    players[x].setValue('score',0)
    players[x].setName("Player " + str(x + 1))
player1 = players[0]
player2 = players[1]
player3 = players[2]
player4 = players[3]

broken = False
firstHand = False
trickSuit = None

def maxScore():
    max = 0
    for p in players:
        sc = p.getValue('score')
        if sc > max: max = sc
    return max

def printScores():
    print("----Scores----")
    for p in players: print(str(p) + "    " + str(p.getValue('score')))



def isValid(card, hand):
    global trickSuit, firstHand, broken
    if card == None:
        return False
    if trickSuit == "passing":
        return True
    suit = card.suit
    if firstHand:
        if trickSuit == None:
            if str(card) == "2 of Clubs":
                trickSuit = "Clubs"
                return True
            else:
                return False
        elif suit == "Hearts":
            return False
    if (not broken) and (trickSuit == None) and (suit == "Hearts"):
        return False
    if trickSuit == None:
        trickSuit = suit
        return True
    if suit == trickSuit:
        return True
    elif Game.suitmatch(hand, trickSuit):
        return False
    else:
        if suit == "Hearts":
            broken = True
        return True


def passCards(passTo):
    global trickSuit, broken
    trickSuit = "Passing"
    if passTo % 4 == 0:
        return
    for x in range(4):
        target = players[(x + passTo) % 4]
        current = players[x]
        for y in range(3):
            passing = current.select("Player " + str(x+1) + ", Pick a card to Pass", current.getContents('hand'))
            current.remove(passing, 'hand')
            target.add(passing, 'cardsTaken')
    for p in players:
        cards = p.getContents('cardsTaken')
        p.addItems(cards,'hand')
        p.removeItems(cards,'cardsTaken')



def getStarter():
    #Iterate over the players
    for p in players:
        #If a player has the two of clubs, this is the starter
        if not Game.getCard(p.getContents('hand'),'2 of Clubs') == None: return p

def trick(startingPlayer):
    global trickSuit
    i = players.index(startingPlayer)
    trickSuit = None
    winner = None
    winCard = None
    for x in range(4):
        print("Cards currently in play")
        for card in PlaySpace.contents('loc'):
            print str(card)
        def isWinner(card):
            if not card.suit == trickSuit:
                return False
            elif card.value > winCard.value:
                return True
        m = (x+i) % 4
        current = players[m]
        hand = current.getContents('hand')
        picked = None
        while not isValid(picked, hand):
            picked = current.select("Player " + str(m+1) + ", Play a card.", hand)
        if x == 0:
            trickSuit = picked.suit
            winner = current
            winCard = picked
        elif isWinner(picked):
            winCard = picked
            winner = current
        current.play(picked, 'loc', 'hand')
    print("Cards currently in play")
    for card in PlaySpace.contents('loc'):
        print str(card)
    winner.takeAll('loc', 'cardsTaken')
    print(str(winner) + " takes the trick")
    return winner

def computeScores():
    for p in players:
        scr = 0
        for card in p.getContents('cardsTaken'):
            print(str(card))
            if card.suit == 'Hearts':
                scr = scr + 1
            elif card.suit == 'Spades' and card.rank == 'Queen':
                scr = scr + 13
        if scr == 26:
            for q in players:
                if p != q:
                    old = q.getValue('score')
                    q.setValue('score', old + 26)
        else:
            old = p.getValue('score')
            p.setValue('score', old + scr)
        p.removeItems(p.getContents('cardsTaken'), 'cardsTaken')




passTo = 0
print("HI THERE")
while maxScore() < 100:
    passTo += 1
    print("IN LOOP")
    d = Game.PokerDeck()
    d.shuffle()
    for p in players:
        p.addItems(d.draw(13),'hand')
#        p.sortItems(Game.sortPokerCards)
    broken = False
    passCards(passTo)
#    for p in players:
#        p.sortItems(Game.sortPokerCards)
    firstHand = True
    startingPlayer = getStarter()
    startingPlayer = trick(startingPlayer)
    firstHand = False
    for x in range(12):
        startingPlayer = trick(startingPlayer)
    computeScores()
    printScores()


